// pages/setuppersonaldata/setuppersonaldata.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rightarrow: app.data.imgurl + "rightarrow.png",
    hedsrc: app.data.imgurl + "headimg.png",
    index: 0,
    array: ["男", "女"],
    region: ['广东省', '广州市', '海珠区'],
    region: ['四川省', '成都市', '青羊区'],
    wenzi: '获取验证码',
    getmyhuiyuaninfo: app.data.url + 'sheyingjia/Xhuiyuan1/getmyhuiyuaninfo',
    getcode: app.data.url + 'sheyingjia/myhuiyuan/getcode',
    addhuiyuan: app.data.url + 'sheyingjia/Xhuiyuan1/addhuiyuan',
    shuju: { address_one: '四川省-成都市-青羊区' }
  },

  //男女选择
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  //选择地址
  bindRegionChange: function (e) {
    var shuju = this.data.shuju;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var value = e.detail.value;
    var str = value[0] + '-' + value[1] + '-' + value[2];
    shuju.address_one = str;
    console.log(shuju);
    this.setData({
      region: e.detail.value,
      shuju: shuju
    })
  },
  //修改电话号码
  toupdatephonenumber: function () {
    wx.navigateTo({
      url: '/pages/updatephonenumber/updatephonenumber',
    })

  },
  //填写数据
  tianshuju:function(e){
    var shuju = this.data.shuju;
    var value = e.detail.value;
    var type = e.currentTarget.dataset.type;
    shuju[type] = value;
    console.log(shuju);
    this.setData({ shuju: shuju });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user = wx.getStorageSync('user');
    var that = this;
    if (!user) {
      wx.showToast({
        title: '请先登录',
      })
      return;
    }
    ea.qinqiu(that.data.getmyhuiyuaninfo, { token: user.token, leixin:2},function(data){
      console.log(data);
      var shuju = data.data;
      if (shuju.length == 0) {
        // console.log(1)

      } else {
        // console.log(2)
        that.setData({ shuju: shuju });
        if (shuju.huiyuan == 2) {
          wx.navigateTo({
            url: '/pages/memberdownloadgroup/memberdownloadgroup',
          })
        } else {
          wx.showToast({
            title: '你已申请个人会员',
          })
        }

      }
    })

  },
  xiazaigeren: function () {
    var shuju = this.data.shuju;
    var that = this;
    var user = wx.getStorageSync('user');
    shuju.token = user.token;
    shuju.huiyuan = 2;
    console.log(shuju);
    ea.request(that.data.addhuiyuan, shuju, function (res) {
      var data = res.data;
      // console.log(data);
      // return;
      if (data.code == 200) {
        wx.navigateTo({
          url: '/pages/memberdownloadgroup/memberdownloadgroup',
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
  },
  //发送验证码
  fasongcode: function () {
    var that = this;
    var shuju = this.data.shuju;
    if (!shuju.phone) {
      wx.showToast({
        title: '请填写手机号码',
        icon:'none'
      })
      return;
    }
    var wenzi = this.data.wenzi;
    if (wenzi != '获取验证码') {
      return;
    }
    wx.showLoading({
      title: '获取中',
    })
    ea.request(that.data.getcode, { phone: shuju.phone }, function (res) {
      wx.hideLoading();
      var data = res.data;
      if (data.code == 200) {
        console.log(data);
        ea.TimeDown(30, function (number) {
          var str = '';
          if (number == 0) {
            str = '获取验证码'
          } else {
            str = number + '秒';
          }
          that.setData({ wenzi: str });
        })
      } else {
        console.log(data);
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})